 /**
 * @file tal_network_mgr.h
 * @brief Common process - network manager
 * @version 1.0.0
 * @date 2021-09-09
 *
 * @copyright Copyright 2018-2021 Tuya Inc. All Rights Reserved.
 *
 */
#ifndef __TAL_NETWORK_MGR_H__
#define __TAL_NETWORK_MGR_H__


#ifdef __cplusplus
    extern "C" {
#endif

#include "tuya_cloud_types.h"
#include "tuya_zigbee_stack.h"

#include "tal_end_device.h"
#include "tal_poll_manager.h"
#include "tkl_network_manager.h"

/*
 * zigbee network status
*/
typedef enum {
    TAL_ZG_NWK_IDLE = 0,            ///< inner using
    TAL_ZG_NWK_POWER_ON_LEAVE,      ///< power on and device is not joined network
    TAL_ZG_NWK_POWER_ON_ONLINE,     ///< power on and device is already joined network
    TAL_ZG_NWK_JOIN_START,          ///< start joining network
    TAL_ZG_NWK_JOIN_TIMEOUT,        ///< network joining timeout
    TAL_ZG_NWK_JOIN_OK,             ///< network joined success
    TAL_ZG_NWK_LOST,                ///< network lost, lost parent
    TAL_ZG_NWK_REJOIN_OK,           ///< network rejoin ok
    TAL_ZG_NWK_REMOTE_LEAVE,        ///< remove device by remote device
    TAL_ZG_NWK_LOCAL_LEAVE,         ///< remove device by local
    TAL_ZG_NWK_MF_TEST_LEAVE,       ///< remove device by PC test tools
    TAL_ZG_NWK_ZLL_JOINED,          ///< network joined zll network
    TAL_ZG_NWK_ZLL_LEAVE,           ///< remove device Zll Reset To Factory New
    TAL_ZG_NWK_OUT_OF_BAND_JOIN_FAIL,///< out_of_band_join_fail
} TAL_ZG_NWK_STATUS_E;

/*
 * zigbee route node config
*/
typedef struct {
    UINT8_T reserved;
} TAL_ZG_ROUTER_CFG_T;

/*
 * zigbee end-device node config
*/
typedef struct {
    TAL_ZG_POLL_CFG_T    poll_config;
    TAL_ZG_REJOIN_CFG_T  rejoin_config;
} TAL_ZG_ED_CFG_T;

/*
 * zigbee sleep end-device node config
*/
typedef struct {
    TAL_ZG_POLL_CFG_T    poll_config;
    TAL_ZG_REJOIN_CFG_T  rejoin_config;
} TAL_ZG_SLEEP_ED_CFG_T;

/*
 * zigbee node config
*/
typedef struct {
    ZG_NODE_TYPE_E              node_type;      ///< device node type(not including coordinator).
    INT8_T                      tx_power;       ///< device radio power.
    ZG_SCAN_DURATION_E          scan_duration;  ///< the time that the RF receiver is turned on on a channel.
    UINT16_T                    scan_interval;  ///< scan interval when joining network(beacon send interval for join)(bet: ms).
    union {
        TAL_ZG_ROUTER_CFG_T    router_cfg;      ///< routing node configuration.
        TAL_ZG_ED_CFG_T        ed_cfg;          ///< end device node(rx open on idle) configuration.
        TAL_ZG_SLEEP_ED_CFG_T  sleep_ed_cfg;    ///< sleep end device(rx close on idle) configuration.
    } config;
} TAL_ZG_NODE_CFG_T;

/*
 * zigbee join config
*/
typedef struct {
    BOOL_T auto_join_power_on_flag;             ///< Whether the system automatically tries to network after starting
    BOOL_T auto_join_remote_leave_flag;         ///< After being deleted by the APP, whether the network automatically
    UINT_T join_timeout;                        ///< Maximum timeout for networking. unit:ms
} TAL_ZG_JOIN_CFG_T;

/*
 * zigbee out of band join config
*/
typedef struct {
    UINT16_T node_id;           ///< node short addr,if 0xffff random generation, otherwise user specified
    UINT8_T channel;            ///< target network channel(11~26)
    INT8_T tx_power;            ///< node tx power value
    ZG_NODE_TYPE_E node_type;   ///< node type
    ZG_NWK_TYPE_E nwk_type;     ///< target network type
    UINT16_T panid;             ///< target network panid
    UINT8_T *ext_panid;         ///< target network extend panid
    UINT8_T *tc_mac;            ///< gateway or trust center mac address
    UINT8_T *nwk_key;           ///< network key
} TAL_OUTOF_BAND_JOIN_T;


/**
 * @brief zigbee leave start after joined network
 * NOTE: the default is enabled network recovery
 * 
 * @param[in] en: TRUE disables the network recovery 
 * @return none
 */
VOID_T tal_zg_nwk_recovery_disable(BOOL_T en);

/**
 * @brief zigbee leave start after joined network
 * NOTE: this API will take effect when joined network
 * 
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_leave_start(VOID_T);

/**
 * @brief zigbee joining start
 * NOTE: this API will leave the network first and then joining network
 *
 * @param[in]   timeout_ms: joining network timeout(bet: second)
 * @return TRUE: start joining network, FALSE: already in joining process
 */
BOOL_T tal_zg_join_start(UINT_T timeout_ms);

/**
 * @brief zigbee out of band join start
 * NOTE: gateway need open network
 *
 * @param[in]   nwk_cfg: point to network configuration information
 * @return TRUE: joined network, FALSE: join fail
 */
BOOL_T tal_zg_nwk_out_of_band_join(TAL_OUTOF_BAND_JOIN_T *nwk_cfg);

/**
 * @brief zigbee node configuration
 * 
 * @param[in]   config: point to node configuration
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_node_config(TAL_ZG_NODE_CFG_T *config);

/**
 * @brief zigbee joining network configuration
 * 
 * @param[in]   config: point to join configuration
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_join_config(TAL_ZG_JOIN_CFG_T *config);

/**
 * @brief zigbee network status
 * 
 * @param[in]   none
 * @return network status
 */
TAL_ZG_NWK_STATUS_E tal_zg_nwk_status_get(VOID_T);

/**
 * @brief zigbee network network change callback
 * @note < this is a weak function you can rewrite this API in app layer >
 * 
 * @param[in]   status: network status
 * @return none
 */
VOID_T tal_zg_nwk_status_changed_callback(TAL_ZG_NWK_STATUS_E status);

/**
 * @brief enable broadcast permit join after joined network(default is disable)
 * NOTE: this interface needs to be enabled if ZigBee 3.0 authentication is performed
 *
 * @param[in]   enable: enable broadcast permit join
 * @return  none
 */
VOID_T tal_zg_enable_permit_join_after_joined(BOOL_T enable);

/**
 * @brief enable mandatory of only using installcode when join network (default is disable)
 * NOTE: None
 *
 * @param[in]   enable: enable only use installcode when join network
 * @return  none
 */
VOID_T tal_zg_nwk_join_by_installcode_only_enable(BOOL_T enable);

/**
 * @brief 
 * 
 * @return TKL_NWK_BASIC_STATE_E 
 */
TKL_NWK_BASIC_STATE_E tal_zg_nwk_base_status_get(VOID_T);
#ifdef __cplusplus
}
#endif

#endif

